package com.nyp.remoter.ejb;

import java.util.concurrent.Future;

import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

/**
 * Session Bean implementation class HelloServiceImpl
 */
@Stateless(mappedName = "helloService")
public class HelloServiceImpl implements HelloServiceLocal {

	@Inject
	Logger log;

	private static String DEFAULT_NAME = "World"; 
	
	public String greet(String name) {
		String finalName = StringUtils.defaultIfEmpty(name, DEFAULT_NAME);
		String greeting =  "Hello "+finalName;
		log.debug(greeting);
		return greeting;
	}
	@Override
	@Asynchronous
	public Future<String> doLongProcess() {
		System.out.println("long process started");
		StringBuilder str = new StringBuilder("");
		try {
			Thread.sleep(3000);
			str.append("First step complete");
			System.out.println(str.toString());
			Thread.sleep(3000);
			str.append("Second step complete");
			System.out.println(str.toString());
			Thread.sleep(3000);
			str.append("Third step complete, done");
			System.out.println(str.toString());
			
		} 
		catch (InterruptedException e) {
			str.append("error");
		}
		return new AsyncResult<String>(str.toString());
	}
}
